<template>
  <div>
    <el-dialog v-model="model" :title="type=='ADD'?'添加用户':'编辑用户'" width="500" :close-on-click-modal="false">
      <el-form :model="form" label-width="80" :rules="rules" ref="formRef">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="选择性别">
            <el-option v-for="(item,index) in genderDrop" :key="index" :value="item.Value" :label="item.Key"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="选择状态">
            <el-option v-for="(item,index) in statusDrop" :key="index" :value="item.Value" :label="item.Key"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="submitForm(formRef)">
            {{type=='ADD'?'添加':'修改'}}
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import router from '@/router'
import { moduleExpression } from '@babel/types'
import user from '@/config/Enums/user'
import Pinterface from '@/config/Interface/Public'
import Interface from '@/config/Interface/User'
import { defineComponent,ref,onMounted,reactive,toRefs} from 'vue'
import { getCurrentInstance} from 'vue'
import { useStore } from '../../store'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import {createUserApi,updateUserApi} from '@/request/user'
export default defineComponent({
  name: 'addUser',
  components: {

  },
  setup(props,{ emit }){
    const store = useStore()
    const context = getCurrentInstance()?.appContext.config.globalProperties; //获取this
    const msg = reactive<any>({})
    const model =ref(false) // 模态框显示隐藏
    const type = ref('') //类型 添加/编辑

    const reactiveData = reactive<Interface.addUserReactiveData>({
      form:{
        username:'',
        mobile:'',
        email:'',
        status:'',
        gender:''
      },
      statusDrop:context?.$enums.GetEnumArr(user.StatusDrop),
      genderDrop:context?.$enums.GetEnumArr(user.GenderDrop),
    })

    const trigger = (Type:string,scope?:any) =>{
      model.value = true
      type.value = Type
      if(scope) {
        msg.value = scope.row
        reactiveData.form.username = msg.value.username
        reactiveData.form.mobile = msg.value.mobile
        reactiveData.form.email = msg.value.email
        reactiveData.form.status = msg.value.status
        reactiveData.form.gender = msg.value.gender
      }

    }


    const checkEmail = (rule: any, value: any, callback: any) => {
        const RegEmail = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
        if(value=='') {
          return callback(new Error('请输入邮箱'))
        }
        if(!RegEmail.test(value)) {
          return callback(new Error('邮箱格式不正确'))
        }else{
          callback()
        }
    }
    const checkMobile = (rule: any, value: any, callback: any) => {
        const RegMobile = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
        if(value==''){
          return callback(new Error('请输入手机号'))
        }
        if(!RegMobile.test(value)) {
          return callback(new Error('请输入正确的手机号'))
        }else{
          callback()
        }
    }
    const rules = reactive<FormRules>({
      username:[
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 4, max: 20, message: '用户名长度4-20', trigger: 'blur' },
      ],
      mobile:[
        { required: true, validator: checkMobile, trigger: 'blur' },
      ],
      email:[
        { required: true, validator: checkEmail, trigger: 'blur' },
      ],
      status:[
        { required: true, message: '请选择状态', trigger: 'change' },
      ],
      gender:[
        { required: true, message: '请选择性别', trigger: 'change' },
      ],
    })
    // 关闭表单
    const close = () => {
      reactiveData.form = new Interface.addUserForm()
      model.value = false;
    }
    const formRef = ref<FormInstance>()
    // 提交表单
    const submitForm = async (formEl: FormInstance | undefined) => {
      if (!formEl) {
        return
      }else{
        await formEl.validate((valid, fields) => {
        console.log(valid);
        if (!valid) {
          return
        } else {
          postSend()
        }
      })
      }
    }

    const postSend = async () =>{
      const postData:any = {
        ...reactiveData.form,
      }
      if(type.value =='ADD'){
        const res = await createUserApi(postData)
        if(res.data.code === 1){
          ElMessage({
            message:"创建成功",
            type:"success"
          })
          close()
          emit('RefreshListEmit')
        }
      }else{
        postData.id = msg.value.id
        const res = await updateUserApi(postData)
        if(res.data.code === 1){
          ElMessage({
            message:"修改成功",
            type:"success"
          })
          close()
          emit('RefreshListEmit')
        }
      }
    }

    return {
      ...toRefs(reactiveData),
      model,type,
      trigger,
      rules,
      submitForm,
      checkEmail,checkMobile,
      close,formRef
    }
  }
})
</script>

<style scoped lang="stylus">
</style>
